
// panel object
local PANEL = {};


/*------------------------------------
	initialize the list view
------------------------------------*/
function PANEL:Init( )

	// disable the canvas painting
	self.pnlCanvas.Paint = function ( self ) return true; end
	
	// custom painting hook
	Derma_Hook( self, "Paint", "Paint", "MXListView" );

end


/*------------------------------------
	add column to list
------------------------------------*/
function PANEL:AddColumn( name, mat, pos )

	// create the column
	local col = vgui.Create( "MXListView_Column", self );
	
	// set the name
	col:SetName( name );
	
	// set the material
	col:SetMaterial( mat );
	
	// set the z position
	col:SetZPos( 10 );

	// check if a position was supplied
	if ( pos ) then
	
		// garry has a TODO? BLASPHEMY!!
		
	else
	
		// get the id
		local ID = table.insert( self.Columns, col );
		
		// store it
		col:SetColumnID( ID );
	
	end
	
	// refresh the layout
	self:InvalidateLayout();
	
	// return the created column
	return col;
	
end


/*------------------------------------
	add a line
------------------------------------*/
function PANEL:AddLine( ... )

	// make the layout dirty (for updating)
	self:SetDirty( true );
	
	// invalidate the layout
	self:InvalidateLayout();
	
	// create the line
	local line = vgui.Create( "MXListView_Line", self.pnlCanvas );
	
	// add the line to the others, get the new ID
	local ID = table.insert( self.Lines, line );
	
	// attach to parent
	line:SetListView( self );
	
	// set the ID
	line:SetID( ID );
	
	// loop through the arguments
	for k, v in pairs( {...} ) do
	
		// add the text
		line:SetColumnText( k, v );
		
	end
	
	// make appear at the bottom of the sorted list
	local SortID = table.insert( self.Sorted, line );
	
	// alternate for even numbered lines
	if ( SortID % 2 == 1 ) then
	
		// enable alt
		line:SetAltLine( true );
		
	end

	// return the created line
	return line;
	
end


/*------------------------------------
	list view has changed in size
------------------------------------*/
function PANEL:OnResized( )

	// make it dirty
	self:SetDirty( true );
	
	// refresh the layout
	self:InvalidateLayout();
	
end

// define
derma.DefineControl( "MXListView", "", PANEL, "DListView" );


// clear panel object
PANEL = {};


// custom paint hook
Derma_Hook( PANEL, "Paint", "Paint", "MXListViewLine" );


/*------------------------------------
	set column text
------------------------------------*/
function PANEL:SetColumnText( i, text )

	// check if we need to create it
	if ( !self.Columns[ i ] ) then
	
		// create it
		self.Columns[ i ] = vgui.Create( "DLabel", self );
		
		// disable mouse
		self.Columns[ i ]:SetMouseInputEnabled( false );
		
		// set the inset
		self.Columns[ i ]:SetTextInset( 3 );
		
		// refresh our layout
		self:InvalidateLayout();
		
	end
	
	// custom skin hook
	Derma_Hook( self.Columns[ i ], "ApplySchemeSettings", "Scheme", "MXListViewLine" );
	
	// update the text
	self.Columns[ i ]:SetText( text );
	self.Columns[ i ].Value = text;
	
end

// copy function
PANEL.SetValue = PANEL.SetColumnText;

// define
derma.DefineControl( "MXListView_Line", "", PANEL, "DListView_Line" );


// panel object
PANEL = {};


/*------------------------------------
	initialize column
------------------------------------*/
function PANEL:Init( )

	// create the header button
	self.Header = vgui.Create( "DButton", self );
	
	// custom derma hooks
	Derma_Hook( self.Header, "Paint", "Paint", "MXListViewHeader" );
	Derma_Hook( self.Header, "ApplySchemeSettings", "Scheme", "MXListViewHeader" );
	
	// setup the click function
	self.Header.DoClick = function() self:DoClick(); end
	
	// setup the double click function
	self.Header.DoRightClick = function() self:DoRightClick(); end
	
	// create the drag bar
	self.DraggerBar = vgui.Create( "DListView_DraggerBar", self );
	
	// set sizes
	self:SetMinWidth( 10 );
	self:SetMaxWidth( 1920 * 10 );

end


/*------------------------------------
	column header was clicked
------------------------------------*/
function PANEL:DoClick( )

	// sort by column
	self:GetParent():SortByColumn( self:GetColumnID(), self:GetDescending() );
	
	// store the decending value
	self:SetDescending( !self:GetDescending() );
	
	// get the parent
	local par = self:GetParent();
	
	// check for the OnSorted event
	if ( par && par.OnSorted ) then
	
		// call it
		par.OnSorted( par );
	
	end

end

// define
derma.DefineControl( "MXListView_Column", "", PANEL, "DListView_Column" );

